package com.lx.oms.controller.service.impl;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lx.oms.common.constant.MsgConstant;
import com.lx.oms.common.entity.Query;
import com.lx.oms.common.entity.R;
import com.lx.oms.common.utils.CommonUtils;
import com.lx.oms.controller.entity.SysCategoryEntity;
import com.lx.oms.controller.manager.SysCategoryManager;
import com.lx.oms.controller.service.SysCategoryService;

/**
 * 类别
 *
 * @author houzhong
 * 
 */
@Service("sysCategoryService")
public class SysCategoryServiceImpl implements SysCategoryService {

	@Autowired
	private SysCategoryManager sysCategoryManager;

	@Override
	public List<SysCategoryEntity> listCategoryByParentId(String categoryId) {
		Query query = new Query();
		query.put("parentId", categoryId);
		List<SysCategoryEntity> areas = sysCategoryManager.listCategoryByParentId(query);
		for(SysCategoryEntity area : areas) {
			area.checkParent();
		}
		return areas;
	}

	@Override
	public R listCategoryByParentId(Map<String, Object> params) {
		Query query = new Query(params);
		List<SysCategoryEntity> areas = sysCategoryManager.listCategoryByParentId(query);
		return CommonUtils.msg(areas);
	}

	@Override
	public R saveCategory(SysCategoryEntity category) {
		int count = sysCategoryManager.saveCategory(category);
		return CommonUtils.msg(count);
	}

	@Override
	public R getCategoryById(BigInteger id) {
		SysCategoryEntity area = sysCategoryManager.getCategoryById(id);
		area.checkParentName();
		return CommonUtils.msg(area);
	}

	@Override
	public R updateCategory(SysCategoryEntity category) {
		int count = sysCategoryManager.updateCategory(category);
		return CommonUtils.msg(count);
	}

	@Override
	public R batchRemoveCategory(String[] id) {
		boolean children = sysCategoryManager.hasChildren(id);
		if(children) {
			return R.error(MsgConstant.MSG_HAS_CHILD);
		}
		int count = sysCategoryManager.batchRemoveCategory(id);
		return CommonUtils.msg(id, count);
	}

	@Override
	public SysCategoryEntity getCategoryByName(String name) {
		Query query = new Query();
		query.put("name", name);
		SysCategoryEntity categoryEntity = sysCategoryManager.getCategoryByName(query);
		return categoryEntity;
	}

	@Override
	public List<SysCategoryEntity> getCategoryByParentId(String parentId,String type) {
		Query query = new Query();
		query.put("parentId", parentId);
		query.put("type", type);
		
		List<SysCategoryEntity> categoryEntity = sysCategoryManager.getCategoryByParentId(query);
		return categoryEntity;
	}

	

}
